package nl.jjsoft.airlock.utils
{
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.resources.ResourceManager;
	
	import org.swizframework.storage.ISharedObjectBean;
	
	public class ResourceFactory implements IResourceFactory
	{
		public static const LOCALE_SO_KEY:String = "locale";
		
		[Inject]
		public var so:ISharedObjectBean;
		
		private var _bundleName:String = "resources";
		
		private var _supportedLocales:Array;
		
		[Bindable]
		public var localeProvider:ArrayCollection;
		
		public var defaultLocale:String = "en_US";
		
		public function set bundleName(value:String):void {
			_bundleName = value;
		}
		
		public function set supportedLocales(value:Array):void {
			_supportedLocales = value;
			initLocaleProvider();		
		}
		
		private function initLocaleProvider():void {
			localeProvider = new ArrayCollection();
			for (var i:int = 0; i < _supportedLocales.length; i++) {
				var locale:Object = new Object();
				locale['label'] = getResource(_supportedLocales[i]);
				locale['data'] = _supportedLocales[i];
				localeProvider.addItem(locale);
			}	
		}
		
		public function get supportedLocales():Array {
			return _supportedLocales;
		}
		
		public function ResourceFactory()
		{
		}

		public function getResource(name:String):String {
			return ResourceManager.getInstance().getString(_bundleName, name);		
		}
		
		public function changeLocale(locale:String):void {
			ResourceManager.getInstance().localeChain = [locale];
			Alert.noLabel = getResource("shared.nolabel");
			Alert.cancelLabel = getResource("shared.yeslabel");
			initLocaleProvider();
		}
	}
}